package yx.action;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;

import yx.service.AddrbookServiceInt;
import yx.vo.AddrbookVO;
import yx.vo.DatagridVO;
import yx.vo.JsonVO;
import yx.vo.UserVO;

import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("basePackage")
@Namespace("/")
@Action(value = "addrbookAction")
public class AddrbookAction extends BaseAction implements ModelDriven<AddrbookVO> {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(AddrbookAction.class);

	private AddrbookVO addrbook = new AddrbookVO();

	@Override
	public AddrbookVO getModel() {
		return addrbook;
	}

	private AddrbookServiceInt addrbookService;

	public AddrbookServiceInt getAddrbookService() {
		return addrbookService;
	}

	@Autowired
	public void setAddrbookService(AddrbookServiceInt addrbookService) {
		this.addrbookService = addrbookService;
	}

	public void queryAddrbook() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		UserVO user = (UserVO) session.getAttribute("LOGIN_USER");
		addrbook.setUserId(user.getId());
		super.writeJson(addrbookService.datagrid(addrbook));
	}

	public void addAddrbook() {
		JsonVO j = new JsonVO();
		try {
			HttpSession session = ServletActionContext.getRequest().getSession();
			UserVO user = (UserVO) session.getAttribute("LOGIN_USER");
			addrbook.setUserId(user.getId());
			AddrbookVO u = addrbookService.save(addrbook);
			j.setSuccess(true);
			j.setMsg("增加成功！");
			j.setObj(u);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}

	public void removeAddrbook() {
		addrbookService.remove(addrbook.getIds());
		JsonVO j = new JsonVO();
		j.setSuccess(true);
		j.setMsg("删除成功！");
		super.writeJson(j);
	}

	public void editAddrbook() {
		JsonVO j = new JsonVO();
		try {
			AddrbookVO u = addrbookService.edit(addrbook);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
			j.setObj(u);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}

	public void queryGroup() {
		DatagridVO datagridVO = addrbookService.groupDatagrid();
		if (datagridVO.getTotal().intValue() != 0) {
			List list = datagridVO.getRows();
			super.writeJson(list);
		}
	}

	public void addGroup() {
		JsonVO j = new JsonVO();
		try {
			AddrbookVO u = addrbookService.saveGroup(addrbook);
			j.setSuccess(true);
			j.setMsg("增加成功！");
			j.setObj(u);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}

}
